<?php

namespace App\Models;

use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\Relations\BelongsTo;

class AfterSalesApproval extends Model
{
    use HasFactory;

    protected $fillable = [
        'request_id',
        'admin_id',
        'action',
        'reason',
        'comment',
        'amount_adjustment',
    ];

    protected $casts = [
        'amount_adjustment' => 'decimal:2',
    ];

    // 操作类型常量
    const ACTION_APPROVE = 'approve';    // 通过
    const ACTION_REJECT = 'reject';     // 拒绝
    const ACTION_PROCESS = 'process';   // 开始处理
    const ACTION_COMPLETE = 'complete'; // 完成
    const ACTION_CANCEL = 'cancel';      // 取消

    /**
     * 关联售后申请
     */
    public function request(): BelongsTo
    {
        return $this->belongsTo(AfterSalesRequest::class, 'request_id');
    }

    /**
     * 关联管理员
     */
    public function admin(): BelongsTo
    {
        return $this->belongsTo(User::class, 'admin_id');
    }

    /**
     * 获取操作文本
     */
    public function getActionTextAttribute(): string
    {
        return match($this->action) {
            self::ACTION_APPROVE => '通过',
            self::ACTION_REJECT => '拒绝',
            self::ACTION_PROCESS => '开始处理',
            self::ACTION_COMPLETE => '完成',
            self::ACTION_CANCEL => '取消',
            default => '未知操作',
        };
    }
}
